﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Platform;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("pos-integral-rule")]
    public class PosIntegralRuleController : ReceiptsBaseController<PosIntegralRule>
    {
        private readonly IPosIntegralRuleService _posIntegralRuleService;

        public PosIntegralRuleController(IPosIntegralRuleService posIntegralRuleService)
        {
            this._posIntegralRuleService = posIntegralRuleService;
        }
        public override async Task<ResponseEntity<PagedResultDto<PosIntegralRule>>> QueryPaging([FromBody] PagingRequestDto dto)
        {
            var data = await base.QueryPaging(dto);
            if (data.Data?.Items?.Any() ?? false)
            {
                data.Data.Items = await this._posIntegralRuleService.AttachWarehouse(data.Data.Items.ToArray());
                //将显示为0，即false的记录剔除
                data.Data.Items = data.Data.Items.Where(x => x.isShow == true).ToArray();
                data.Data.TotalCount = data.Data.Items.Count;
            }
            return data;
        }

        [HttpPost("save-multiple")]
        public async Task<ResponseEntity<PosIntegralRule[]>> SaveMultiple([FromBody] ReceiptNoItemsDto<PosIntegralRule> data)
        {
            var res = await this.Repo.SaveReceiptItems(this.HttpContext.RequestServices, data, "PIR");
            return new ResponseEntity<PosIntegralRule[]>().SetData(res.Items);
        }

        [HttpPost("query-by-receipt-no")]
        public async Task<ResponseEntity<PosIntegralRule[]>> QueryByReceiptNo([FromBody] ReceiptNoDto dto)
        {
            dto.Should().NotBeNull();
            dto.receiptNo.Should().NotBeNullOrEmpty();
            var query = this.SingleStoreService.ApplyToQueryable(this.Repo);
            var data = await query.Where(x => x.receiptNo == dto.receiptNo).ToArrayAsync();

            return new ResponseEntity<PosIntegralRule[]>().SetData(data);
        }
        [HttpPost("get-posintegral-rule")]
        public async Task<ResponseEntity<PosIntegralRule[]>> GetPosIntegralRules([FromBody] GetPosIntegralRuleDto dto)
        {
            dto.Should().NotBeNull();
            dto.Page ??= 1;
            (dto.Page >= 1).Should().BeTrue();

            var db = await this.Repo.GetDbContextAsync();

            var query = db.Set<PosIntegralRule>().IgnoreQueryFilters().AsNoTracking();

            if (dto.UpdateTime != null)
            {
                query = query.Where(x => x.updateTime >= dto.UpdateTime.Value);
            }

            query = query.OrderBy(x => x.updateTime).Page(dto.Page.Value, pageSize: 100);

            var data = await query.ToArrayAsync();

            return new ResponseEntity<PosIntegralRule[]>().SetData(data);
        }
    }
}
